<?php

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\exception\CategoryException;
use app\api\exception\MissException;
use app\api\exception\SuccessMessage;
use app\api\model\Category as CategoryModel;
use app\api\validate\IDMustBePositiveInt;
use think\facade\Route;

class Category extends BaseController
{

    /**
     * 获取全部类目列表，但不包含类目下的商品
     * Request 演示依赖注入Request对象
     * @url /category/all
     * @return array of Categories
     * @throws MissException
     */
    public function getAllCategories()
    {
        $category_collection = CategoryModel::getAllInfo();
        if ($category_collection->isEmpty())
        {
            $exception_option = [
                'msg' => '没有任何类目',
                'errorCode' => 50000,
            ];
            throw new MissException($exception_option);
        }
        return $category_collection;
    }

    /**
     * 获取类目基本属性和类目下商品
     * @url /category/:id/products
     * @return Category single
     * @throws MissException
     */
    public function getCategories($id)
    {
        $validate = new IDMustBePositiveInt();
        $validate->goCheck();
        $category_one = $category_collection = CategoryModel::getOneDetail($id);
        if (empty($category_one))
        {
            $exception_option = [
                'msg' => '该类目不存在',
            ];
            throw new MissException($exception_option);
        }
        return $category_one;
    }

}
